<script type="text/javascript">
$(document).ready(function(){
	$("#frmSlide").submit(function(){
		var msg = "";
		if($("#src").attr("value") == ""){
			msg +="Thiếu đường dẫn của ảnh.\n";
		}
		if(msg != ""){
			alert(msg);
			return false;
		}
	});
	//xóa
	//xóa
	$("#btnDelete").click(function(){
		var valid= false;
		var arrChkId = $(".chkId");
		var strIDs = "";
		for(var i =0; i < arrChkId.length; i++){
			if(arrChkId.eq(i).attr("checked")){
				valid = true;
				if(strIDs!=""){
					strIDs +=","+ arrChkId.eq(i).attr("value");
				}else{
					strIDs +=arrChkId.eq(i).attr("value");
				}
				//break;
			}
		}
		if(valid){
			if(confirm("Bạn thực sự muốn xóa những ảnh này?")){
				$("#hddIDs").attr("value", strIDs);
				$("#frmActions").attr("action", "<?php echo $this->url(array(), 'admin_banner_delete');?>");
			}else{
				return false;
			}
		}else{
			alert("Xin hãy chọn ảnh muốn xóa!");
			return false;
		}
	});
});
function BrowseServer()
{
	// You can use the "CKFinder" class to render CKFinder in a page:
	var finder = new CKFinder() ;
	finder.basePath = '<?php echo PUBLIC_URL."/ckfinder/";?>' ;	// The path for the installation of CKFinder (default = "/ckfinder/").
	finder.selectActionFunction = function(fileUrl){	
		document.getElementById( "src" ).value = fileUrl ;
		document.getElementById( "image" ).src = fileUrl ;
	}
	finder.popup() ;
}
</script>
<div id="r-heading">
	<img src="<?php echo PUBLIC_URL.'/admin/images/category.png';?>" alt=""/>
	<span>Quản lý image</span>
</div>
<?php
//nếu có message thì hiển thị
if(!empty($this->message)){
	echo '<div id="message">'.$this->message.'</div>';
} 
?>
<div style="width:40%; float:left;">
	<form id="frmSlide" method = "post" action="">
		<?php if(isset($this->image)){
		?>
		<div class = "row">
			<label for="src">Đường dẫn của ảnh:</label>
			<input type="text" name="src" id="src" size= "50" value="<?php echo $this->image->src;?>"/>
			<input type="button" class="button" value = "Chọn Ảnh" onclick="BrowseServer();"/>
			<img id="image" style="height: auto; width: 330px;" alt="" src="<?php echo $this->image->src;?>"/>
		</div>
		<div class = "row">
			<label for="logo">Mô tả:</label>
			<textarea name = "caption" id="caption"><?php echo $this->image->caption;?></textarea>
		</div>
		<div class = "row">
			<label for="link">Link tới:</label>
			<input type="text" name="link" id="link" size= "50" value="<?php echo $this->image->link;?>"/>
		</div>
		<div class = "row">
			<label for="priority">Thứ tự:</label>
			<input type="text" name="priority" id="priority" size= "10" value="<?php echo $this->image->priority;?>"/>
		</div>
		<?php
		}else{
		?>
		<div class = "row">
			<label for="src">Đường dẫn của ảnh:</label>
			<input type="text" name="src" id="src" size= "50" value=""/>
			<input type="button" class="button" value = "Chọn Ảnh" onclick="BrowseServer();"/>
			<img id="image" style="height: auto; width: 330px;" alt="" src=""/>
		</div>
		<div class = "row">
			<label for="logo">Mô tả:</label>
			<textarea name = "caption" id="caption"></textarea>
			
		</div>
		<div class = "row">
			<label for="link">Link tới:</label>
			<input type="text" name="link" id="link" size= "50" value=""/>
		</div>
		<div class = "row">
			<label for="priority">Thứ tự:</label>
			<input type="text" name="priority" id="priority" size= "10" value=""/>
		</div>
		<?php
		}
		?>
		<div class="row">
			<input id="btnSubmit" type="submit" value="Lưu" class="button"/>
			<input type="reset" value="Làm lại" class="button"/>
		</div>
	</form>
</div>
<div style="width: 60%; float: left;">
	<form id="frmActions" action="" method="post">
		<div id="actions">
			<input type="button" id="btnCreate" class="button" value="Thêm mới" onclick="location = '<?php echo $this->url(array("id" => ""), 'admin_banner');?>';"/>
			<input type="submit" id="btnDelete" class="button" value="Xóa các mục đã chọn"/>
			<input type="hidden" id="hddIDs" value="" name="ids"/>
		</div>
		
		<table class="list" cellspacing="0">
			<thead>
				
				<tr>
					<td width="5%">
						<input type="checkbox" class="chkAll"/>
					</td>
					<td width="30%">
						Ảnh
					</td>
					<td width="20%">
						Mô tả
					</td>
					<td width="20%">
						Link	
					</td>
					<td width="10%">
						Thứ tự	
					</td>
					<td width="13%">
						Thao tác
					</td>
				</tr>
			</thead>
			<tbody>
				<?php
					$count = count($this->arrImages);
					if($count == 0){
				?>
					<tr>
						<td colspan="4" style="color: #fc0202;">
							Chưa có image nào trong cơ sở dữ liệu!
						</td>
					</tr>
				<?php
					}else{
						for($i = 0; $i< $count; $i++){
				?>
					<tr <?php if($i == $count - 1) echo 'class="last"';?>>
						<td width="5%">
							<input type="checkbox" class="chkId" value = "<?php echo $this->arrImages[$i]->id;?>"/>
						</td>
						<td style="padding: 3px;">
							<a  href="<?php echo  $this->url(array('id' => $this->arrImages[$i]->id), 'admin_banner');?>" title="Chỉnh sửa thông tin ảnh">
								<img src="<?php echo $this->arrImages[$i]->src;?>" alt="anthaiauto" width="200px;"/>
							</a>
						</td>
						<td>
								<?php
									echo $this->arrImages[$i]->caption;
								?>
						</td>
						<td>
							<a href="<?php echo $this->arrImages[$i]->link;?>" target="_blank"><?php echo $this->arrImages[$i]->link;?></a>
						</td>
						<td>
							<?php echo $this->arrImages[$i]->priority;?>
						</td>
						<td align="center">
							<a href="<?php echo $this->url(array('id' => $this->arrImages[$i]->id), 'admin_banner');?>">Sửa</a>
							|
							<a href="<?php echo $this->url(array('id' => $this->arrImages[$i]->id), 'admin_banner_delete');?>" onclick="if(!confirm('Bạn thực sự muốn xóa ảnh này?')) return false;">Xóa</a>
						</td>
					</tr>
				<?php
						}
					}
				?>
			</tbody>
			<tfoot>
				<tr>
					<td width="5%">
						<input type="checkbox" class="chkAll"/>
					</td>
					<td>
						Ảnh
					</td>
					<td>
						Mô tả	
					</td>
					<td>
						Link	
					</td>
					<td>
						Thứ tự	
					</td>
					<td>
						Thao tác
					</td>
				</tr>
			</tfoot>
		</table>
	</form>
</div>